package cn.itsource.pethome.org.mapper;

import cn.hutool.core.util.StrUtil;
import cn.itsource.pethome.base.domain.BaseQuery;
import cn.itsource.pethome.org.domain.Department;
import org.apache.ibatis.annotations.Update;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

public interface DepartmentMapper extends Mapper<Department> {

    /**
     * mapper高级查询
     * @param query
     * @return
     */
    default List<Department> findPage(BaseQuery query){
        //1.创建一个 example
        Example example = new Example(Department.class);
        //2.获取条件
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("del",Boolean.TRUE);
        //StrUtil.isBlank 除了判断是否为null 还判断当前的数据类型 以及是否为""
        if (StrUtil.isBlank(query.getKeyword())){
            return this.selectByExample(example);
            //3条件查询
        }else {
            criteria.andLike("name","%"+query.getKeyword()+"%");
            return this.selectByExample(example);
        }



    }

    /**
     * 这个是xml版本
     * @param ids
     */
    void deleteBatch(List<Long> ids);

    /**
     * 循环ids的写法
     * @param ids
     */
    default void deleteBatch1(List<Long> ids){
        Department dept = new Department();
        ids.stream().forEach(id ->{
                dept.setId(id);
                dept.setDel(Boolean.FALSE);
                this.updateByPrimaryKey(dept);
                }
        );
    };

    /**
     * 使用 tkmapper的Example写法
     * @param ids
     */
    default void deleteBatch2(List<Long> ids){
        Department dept = new Department();
        dept.setDel(Boolean.FALSE);
        //1.获取example
        Example example = new Example(Department.class);
        //2,获取条件
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id",ids);
        this.updateByExampleSelective(dept,example);

    };

    /**
     * 直接写sql
     * @param ids
     * 这种写法是最恶心的
     */
    @Update("  update  t_department set del = 0  where id in #{ids}")

    void deleteBatch3(List<Long> ids);
}
